// pages/components/Collect/collect.js
// 调用时传递两个参数 一个是顶部的名字，一个是类型（移动/收藏）
import { request,fomatForCollect } from '../../../utils/util'

const { $Message } = require('../../../dist/base/index');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNeedAction:false, //用来判断是否显示三个点
    fileList: [],
    isLoading:true,
    classifyType: "",
    key:"",
    fileListHeight:app.globalData.fileListHeight + "px",
    fileKey:"",//文档或文件的id或key
    backPageClassify:""//返回时需用到的从哪进来的参数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      const classifyType = options.classifyType
      const key = options.key
      const backPageClassify = options.backPageClassify
      
      
      this.setData({
        ...this.data,
        classifyType:classifyType,
        key:key,
        backPageClassify:backPageClassify
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getFileList();
      wx.setStorageSync('location', "collect")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    /**
   * 获取数据的封装
   */
  getFileList(){
    const that = this
     //key区分根目录还是不是根目录
    if(this.data.key === undefined){
      const  param = {
        type:this.data.classifyType,
      }
      request("GET","/api/service/catalog",param).then(res =>{
        const data = res.data.data;
        const newData = fomatForCollect(that.filter(data))
        
        that.setData({
          fileList:newData,
          isLoading:false
        })
        
    })
    }else{ //不是根目录
      const  param = {
        type:this.data.classifyType,
        parent:this.data.key
      }
      request("GET","/api/service/catalog",param).then(res =>{
        const data = res.data.data;
        const newData =  fomatForCollect(that.filter(data))
        that.setData({
          fileList:newData,
          isLoading:false
        })
       
    })
    }  
  },
  /**
   * 新建文件夹
   */
  createFolder(){
    this.selectComponent("#inputModal").setShow()
  },
  /**
   * 空方法，为了不修改结构
   */
  handleCancel1(){
  
  },
  /**
   * 确定
   */
  sure(){
      //key区分根目录还是不是根目录
      const type = app.globalData.collect.type
      const fileKey = app.globalData.collect.fileKey
      const that = this
      const page = getCurrentPages()
      let num = "";
      if (this.data.backPageClassify === "org") {
        num = page.length - 1;
      };
      if(this.data.backPageClassify === "project"){
        num = page.length - 2
      }
      if (this.data.backPageClassify === "user") {
  
        num = page.length - 2
      }
      if(this.data.key === undefined){ 
        if(type == "doc"){
          const param = {
            doc:fileKey
          }
          request("POST","/api/service/collection/",param,"",true,"收藏中，请稍等")
          .then(res =>{
            if(res.statusCode === 200){
              wx.navigateBack({
                delta: num,
                success:res =>{
                  const showMsg = setTimeout(() => {
                    $Message({
                      content: '收藏成功',
                      type: 'success',

                    });
                    clearTimeout(showMsg)
                  }, 500)
                }
              })
              // wx.switchTab({
              //   url: '../../tabBar/enterprise/enterprise',
              //   success:res =>{
              //     $Message({
              //       content: '收藏成功！',
              //       type: 'success'
              //     });
              //   }
              // })
            }
          })
        }else{
          const param = {
            catalog:fileKey
          }
          request("POST","/api/service/collection/",param,"",true,"收藏中，请稍等")
          .then(res =>{
            if(res.statusCode === 200){
              wx.navigateBack({
                delta: num,
                success:res =>{
                  const showMsg = setTimeout(() => {
                    $Message({
                      content: '收藏成功',
                      type: 'success',

                    });
                    clearTimeout(showMsg)
                  }, 500)
                }
              })
              // wx.switchTab({
              //   url: '../../tabBar/enterprise/enterprise',
              //   success:res =>{
              //     $Message({
              //       content: '收藏成功！',
              //       type: 'success'
              //     });
              //   }
              // })
            }
          }) 
        }
    }else{
      if(type == "doc"){
        const param = {
          catalog_collect:this.data.key,
          doc:fileKey
        }
        request("POST","/api/service/collection/",param,"",true,"收藏中，请稍等")
        .then(res =>{
          if(res.statusCode === 200){
            wx.navigateBack({
              delta: num,
              success:res =>{
                const showMsg = setTimeout(() => {
                  $Message({
                    content: '收藏成功',
                    type: 'success',

                  });
                  clearTimeout(showMsg)
                }, 500)
              }
            })
            // wx.switchTab({
            //   url: '../../tabBar/enterprise/enterprise',
            //   success:res =>{
            //     $Message({
            //       content: '收藏成功！',
            //       type: 'success'
            //     });
            //   }
            // })
          }
        })
      }else{
        const param = {
          catalog_collect:this.data.key,
          catalog:fileKey
        }
        request("POST","/api/service/collection/",param,"",true,"收藏中，请稍等")
        .then(res =>{
          if(res.statusCode === 200){
            wx.navigateBack({
              delta: num,
              success:res =>{
                const showMsg = setTimeout(() => {
                  $Message({
                    content: '收藏成功',
                    type: 'success',

                  });
                  clearTimeout(showMsg)
                }, 500)
              }
            })
            // wx.switchTab({
            //   url: '../../tabBar/enterprise/enterprise',
            //   success:res =>{
            //     $Message({
            //       content: '收藏成功！',
            //       type: 'success'
            //     });
            //   }
            // })
          }
        }) 
      }
    }
  },
  /**
   * 过滤器
   */
  filter(data){
    const newData = [];
    data.forEach(element => {
      if(element.type === "collect"){
          newData.push(element)
      }
    });
    return newData
  }
    
    
})